﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LCA.Utils;
using Composite.Data;
using System.Transactions;
using Composite.Data.Transactions;

public partial class Admin_LCASystem_ManageSystems : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ShowSearchFilter();
        }

        ctlPaginator.ControlToPaginate = ctlSystemList;
    }

    protected void ctlUserSearchFilter_SubmitClicked(object sender, System.EventArgs e)
    {
        ctlSystemList.SearchParameters = ctlSystemSearchFilter.SearchParameters;
        ShowResult();
    }

    protected void btnCreateNew_Click(object sender, EventArgs e)
    {
        PageHelper.AdminPageRedirect(Page, "CreateSystem.aspx");
    }

    protected void btnNewSearch_Click(object sender, EventArgs e)
    {
        PageHelper.AdminPageRedirect(Page, "ManageSystems.aspx");
    }

    private void ShowSearchFilter()
    {
        pnlSearchFilter.Visible = true;

        ctlSystemSearchFilter.Visible = true;

        pnlResult.Enabled = false;
        pnlResult.Visible = false;

        ctlPaginator.Visible = false;
        plhFooter.Visible = false;
    }

    private void ShowResult()
    {
        pnlSearchFilter.Visible = false;

        pnlResult.Enabled = true;
        pnlResult.Visible = true;

        ctlSystemList.RefreshComponents();

        ctlPaginator.ItemCount = ctlSystemList.GetItemCount();
        ctlPaginator.Visible = (ctlPaginator.ItemCount > 0);
        plhFooter.Visible = true;
    }

    protected void btnDeleteSelected_Click(object sender, EventArgs e)
    {
        IList<Guid> flowIds = ctlSystemList.GetSelectedIds();

        using (TransactionScope transactionScope = TransactionsFacade.CreateNewScope())
        {
            using (DataConnection connection = new DataConnection())
            {
                foreach (Guid flowId in flowIds)
                {
                    var system = connection.Get<LCA.Data.LCASystem>().Where(f => f.Id == flowId).FirstOrDefault();
                    system.IsDeleted = true;

                    connection.Update<LCA.Data.LCASystem>(system);

                    
                }

            }

            transactionScope.Complete();
        }

        ctlSystemList.RefreshComponents();
    }
}